package gui;

import java.awt.Color;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.Image;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.net.URL;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import javax.swing.Box;
import javax.swing.BoxLayout;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.ListSelectionModel;
import javax.swing.border.LineBorder;

import model.HojaDespacho;
import model.HojaProduccion;
import model.Pudin;

import org.joda.time.DateTime;
import org.joda.time.format.DateTimeFormat;
import org.joda.time.format.DateTimeFormatter;


public class DespachosPanel extends JPanel implements ActionListener {
	
	DateTime dia;
	ArrayList<ArrayList<JTable>> allTables;
	JButton despachosPudines;
	JButton despachosPostres;
	String puntoDeVenta;
	JList list;
	
	

	DespachosPanel(JFrame parent, DateTime dia, ArrayList<ArrayList<JTable>> allTables) {
		this.dia = dia;
		this.setLayout(new BoxLayout(this, BoxLayout.Y_AXIS));
		this.allTables = allTables;
		puntoDeVenta = "ninguno";
		createMainPanel();
	}
	
	public JPanel createMiddlePanel() {
			
		URL imgURL = ClassLoader.getSystemResource("images/camion.gif");
		Image image = Toolkit.getDefaultToolkit().getImage(imgURL);

		ImageIcon camionIcon = new ImageIcon(image);

		Image img = camionIcon.getImage();  
		Image newimg = img.getScaledInstance(200, 150,  java.awt.Image.SCALE_SMOOTH);  
		camionIcon = new ImageIcon(newimg);
		
		
		list = new JList(new String[] {"Bocagrande",
				"Centro", "Homecenter", "Plazuela", "Ejecutivos 67",
				"Martinez", "Ejecutivos 69","Dulceria" });
		
		list.setSelectionMode(ListSelectionModel.SINGLE_INTERVAL_SELECTION);

		JScrollPane listScroller = new JScrollPane(list);
		//listScroller.setPreferredSize(new Dimension(80, 50));
		
		JButton selectButton = new JButton("Seleccionar");
		selectButton.setActionCommand("seleccionar");
		selectButton.addActionListener(this);
		
		JLabel localLabel = new JLabel("Punto de Venta: " + puntoDeVenta);
		localLabel.setPreferredSize(new Dimension(200,50));
		DateTimeFormatter fmt = DateTimeFormat.forPattern("dd/MM/yyyy");
		String str = fmt.print(dia);
		JLabel diaLabel = new JLabel("Fecha: " + str);
		
		
		JPanel middlePanel = new JPanel();
		middlePanel.setLayout(new BoxLayout(middlePanel, BoxLayout.X_AXIS));
		middlePanel.add(Box.createHorizontalGlue());
		middlePanel.add(new JLabel(camionIcon));
		middlePanel.add(Box.createHorizontalGlue());	
		
		JPanel puntosPanel = new JPanel();
		puntosPanel.setLayout(new BoxLayout(puntosPanel,BoxLayout.Y_AXIS));
		puntosPanel.add(listScroller);
		puntosPanel.add(Box.createVerticalGlue());
		puntosPanel.add(selectButton);
		selectButton.setAlignmentX(Component.CENTER_ALIGNMENT);
		
		middlePanel.add(puntosPanel);
		
		middlePanel.add(Box.createHorizontalGlue());
		
		JPanel infoPanel = new JPanel();
		
		infoPanel.setLayout(new BoxLayout(infoPanel,BoxLayout.Y_AXIS));
		infoPanel.add(localLabel);
		infoPanel.add(diaLabel);
		
		middlePanel.add(infoPanel);		
		middlePanel.add(Box.createHorizontalGlue());	

		return middlePanel;
	}

	public JPanel createLowerPanel() {
		
		URL imgURL = ClassLoader.getSystemResource("images/pdf.png");
		Image image = Toolkit.getDefaultToolkit().getImage(imgURL);


		ImageIcon pdficon = new ImageIcon(image);

		Image img = pdficon.getImage();  
		Image newimg = img.getScaledInstance(20, 20,  java.awt.Image.SCALE_SMOOTH);  
		pdficon = new ImageIcon(newimg);
		
		
		JPanel lowerPanel = new JPanel();
		lowerPanel.setLayout(new BoxLayout(lowerPanel, BoxLayout.X_AXIS));
		lowerPanel.add(Box.createHorizontalGlue());
		
		despachosPudines = new JButton("Hoja de Despachos Pudines",pdficon);
		despachosPudines.addActionListener(this);
		despachosPudines.setActionCommand("despachosPudines");
		
		despachosPostres = new JButton("Hoja de Despachos Postres",pdficon);
		despachosPostres.addActionListener(this);
		despachosPostres.setActionCommand("despachosPostres");
		
		lowerPanel.add(despachosPudines);
		lowerPanel.add(Box.createHorizontalGlue());
		lowerPanel.add(despachosPostres);
		lowerPanel.add(Box.createHorizontalGlue());

		lowerPanel.setBorder(new LineBorder(Color.GRAY));
		
		return lowerPanel;
	}
	
	
	public void createMainPanel() {
		this.removeAll();

		this.add(Box.createVerticalGlue());	
		this.add(createMiddlePanel());
		this.add(Box.createVerticalGlue());	
		this.add(createLowerPanel());
		
		this.validate();
		this.repaint();
	}

	@Override
	public void actionPerformed(ActionEvent arg0) {
		if(arg0.getActionCommand().equals("seleccionar")) {
			puntoDeVenta = (String) list.getSelectedValue();
			createMainPanel();
		}
		else if(arg0.getActionCommand().equals("despachosPudines")) {
			
			if(puntoDeVenta.equals("ninguno")) {
				JOptionPane.showMessageDialog(null,
						"Por favor seleccione un punto de venta",
						"Error de Estado",
						JOptionPane.ERROR_MESSAGE);
			}
			else {
				JFileChooser fc = new JFileChooser();
				int returnVal = fc.showDialog(this, "Guardar");
				
				if (returnVal == JFileChooser.APPROVE_OPTION) {
					   File file = fc.getSelectedFile();
					   HojaDespacho hojaDespacho = new HojaDespacho();
					   
					   List<String> sabores = new ArrayList<String>();
					   sabores.addAll(Arrays.asList(Pudin.BaseSabor.getVainilla()));
					   sabores.addAll(Arrays.asList(Pudin.BaseSabor.getChocolate()));
					   
					   List<String> formas = new ArrayList<String>();
					   formas.addAll(Arrays.asList(Pudin.PesoForma.getPudines()));
					   
					   hojaDespacho.crearHoja(file,sabores,formas,allTables,dia,puntoDeVenta);
				}
			}
		}
		else if(arg0.getActionCommand().equals("despachosPostres")) {
			
			if(puntoDeVenta.equals("ninguno")) {
				JOptionPane.showMessageDialog(null,
						"Por favor seleccione un punto de venta",
						"Error de Estado",
						JOptionPane.ERROR_MESSAGE);
			}
			else {
				JFileChooser fc = new JFileChooser();
				int returnVal = fc.showDialog(this, "Guardar");
				
				if (returnVal == JFileChooser.APPROVE_OPTION) {
					   File file = fc.getSelectedFile();
					   HojaDespacho hojaDespacho = new HojaDespacho();
					   
					   List<String> sabores = new ArrayList<String>();
					   sabores.addAll(Arrays.asList(Pudin.BaseSabor.getPostre()));
					   sabores.addAll(Arrays.asList(Pudin.BaseSabor.getPie()));
					   sabores.addAll(Arrays.asList(Pudin.BaseSabor.getHelado()));

					   List<String> formas = new ArrayList<String>();
					   formas.addAll(Arrays.asList(Pudin.PesoForma.getPostres()));
					   
					   hojaDespacho.crearHoja(file,sabores,formas,allTables,dia,puntoDeVenta);
				}
			}
		}
	}
}
